//
//  LiveNoticeViewController.m
//  YouinRTCDemo
//
//  Created by super on 2021/11/12.
//

#import "LiveNoticeViewController.h"
#import "LiveNoticeTableViewCell.h"
#import "LiveNoticeHeader.h"

@interface LiveNoticeViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>
@property(nonatomic,strong)LiveNoticeHeader *header;
@end

@implementation LiveNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    UIView *bgView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:bgView];
    [self setTabLe];
    [self getData];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [bgView addGestureRecognizer:tap];
    
}


-(void)getData{
    [[YouinRTCManager shardManager] getHistoyNotice:^(NSArray<LiveNoticeModel *> * _Nonnull content) {
        self.noticeModelArray = [NSMutableArray arrayWithArray:content];
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"");
    }];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
}

-(void)tap{
    [self.view endEditing:true];
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 400);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
}

-(void)show{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT-390, SCREEN_WIDTH, 400);
    }];
}

-(void)setTabLe{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(15, SCREEN_HEIGHT, SCREEN_WIDTH-30, 400) style:UITableViewStylePlain];
    _tableView.backgroundColor = rgba(245, 246, 250, 1);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = false;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.estimatedRowHeight = 50;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    [_tableView registerClass:[LiveNoticeTableViewCell class] forCellReuseIdentifier:@"LiveNoticeTableViewCell"];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-30, 30)];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _tableView.layer.cornerRadius = 10;
    _tableView.layer.masksToBounds = YES;

    _header = [[LiveNoticeHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    __weak typeof(self) weakSelf = self;
    _header.block = ^(NSString *str) {
        __strong typeof(self) strongSelf = weakSelf;
        if(weakSelf.block){
            weakSelf.block(str);
        }
        [strongSelf tap];
        [strongSelf getData];
    };
    _header.inputTextView.delegate = self;
    _tableView.tableHeaderView = _header;

    [self.view addSubview:_tableView];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 7;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.noticeModelArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    LiveNoticeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LiveNoticeTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.noticeModel = self.noticeModelArray[indexPath.section];
    return  cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    _header.model.content = self.noticeModelArray[indexPath.section].content;
    [_header sendModel];
}


- (void)textViewDidBeginEditing:(UITextView *)textView{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT-460, SCREEN_WIDTH, 400);
    }];
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, SCREEN_HEIGHT-390, SCREEN_WIDTH, 400);
    }];
}


@end
